<template>

  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->

      <div class="avatar_box">
        <img
          src="../assets/logo.png"
          alt=""
          srcset=""
        >
      </div>
      <!-- 表单区域 -->
      <!-- :model 为表单绑定的对象 :rules="rules" 为验证规则 -->
      <el-form
        label-width='0px'
        class='form_box'
        :model='form'
        :rules="LoginRules"
        ref="ruleForm"
      >
        <!-- 用户名 的验证规则在 formitem 上 -->
        <el-form-item prop='username'>
          <el-input
            prefix-icon="iconfont icon-users"
            v-model='form.username'
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop='password'>
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model='form.password'
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class='btns_box'>
          <el-button
            type='primary'
            @click="login('ruleForm')"
          >登录</el-button>
          <el-button
            type='info'
            @click="reset('ruleForm')"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
export default {
  // eslint-disable-next-line space-before-function-paren
  data() {
    return {
      // 表单数据绑定对象
      form: {
        username: 'admin',
        password: '123456'
      },
      // 表单的验证规则
      LoginRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    reset(formName) {
      // console.log(this)
      // 重置清空的是验证规则 因为文本框的数据是双向绑定在form属性中的，需要手动改变
      this.$refs[formName].resetFields()
      this.form.username = ''
      this.form.password = ''
    },
    login(formName) {
      // console.log(this)
      this.$refs[formName].validate(async (valid) => {
        console.log(valid)
        // 表单的二次校验
        if (!valid) return null
        // const result = await this.$http.post('login', this.form)
        // 解构数据
        const { data: result } = await this.$http.post('login', this.form)
        console.log(result)
        if (result.meta.status !== 200) return this.$message.error('登录失败，请检查账户密码')
        // console.log('登陆成功')
        this.$message.success('登录成功')
        // 将登录后成功的token 保存在缓存中 记录成功的标志
        window.sessionStorage.setItem('token', result.data.token)
        // 通过编程式导航动态跳转
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  height: 300px;
  width: 450px;
  border-radius: 3px;
  background-color: #eee;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #eee;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #8a7d7d26;
  }
}

.btns_box {
  // 改为弹性盒模型
  display: flex;
  // 横轴上 居右
  justify-content: flex-end;
}
.form_box {
  // 居于最下方
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
